package com.exam.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.exam.common.vo.ExamStuVO;
import com.exam.common.vo.ExamTeacherVO;

public class StudentInteceptor extends HandlerInterceptorAdapter {

	private final static String[] IGNORE_URL = {"/subjectList","/grade"};
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean isStudent = false;
		String currentUrl = request.getRequestURL().toString();
		System.out.println(currentUrl);
		for(String url : IGNORE_URL){
			if(currentUrl.contains(url)){
				isStudent = true;
				break;
			}
		}
		
		if(!isStudent){
			ExamStuVO stuVO = (ExamStuVO) request.getSession().getAttribute("stuVO");
			ExamTeacherVO teacher = (ExamTeacherVO) request.getSession().getAttribute("teacher");
			if(stuVO!=null&&teacher==null){
				if(currentUrl.contains("/tea")){
					response.sendRedirect(currentUrl);
					System.out.println("tea");
					isStudent = false;
				}
			}
		}
		return isStudent;
	}

	
}
